// pages/wolk/index.js
let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: '/assets/img/avatar.png',
    nickname: '',
    wolk_num:"click",
    score:"",
    on_off:"0",
    texts:'',
    randomlist:[],
    page:1,
    red:0,
    user_score:0,
    banner:'',
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
    duration: 800 // 滑动动画时长
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (App.globalData.userInfo == ''){
      wx.reLaunch({
        url: '../my/index'
      })
    }
    wx.getWeRunData({
      success (res) {
        // 拿 encryptedData 到开发者后台解密开放数据
        const encryptedData = res.encryptedData
        App._post('wolk/getWolkdata', { encryptedData: encryptedData,iv:res.iv}, function (result) {
          console.log('看谁先后')
        //  wx.showModal({
        //   title: '温馨提示',
        //   content: result.msg,
        //   showCancel: false,
        //   success: function (res) {
        //    return true;
        //   }
        // });
         that.setData({
           wolk_num:result.data.wolk_num,
           score:result.data.score,
           on_off:result.data.on_off,
           //textstexts:result.data.text,
           red:result.data.red,
           user_score:result.data.user_score,
           banner:result.data.bannerlist
         })
         if (App.globalData.userInfo) {
          that.setData({ 
            avatar: App.globalData.userInfo.avatar,
            nickname: App.globalData.userInfo.nickname
          });
        };
        });
      }
    });
    App._post('index/scoreGoods',{page:1,type:2},function(res){
      if(res.code === 1){
        if(res.data.Randomlist.length === 0){
          // that.setData({
          //   text:"已经到底了"
          //  }) 
        }else{
          that.setData({
            randomlist:res.data.Randomlist,
            page:1
          })  
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    // var timestamp = Date.parse(new Date());
    // timestamp = timestamp / 1000;
    //获取缓存时间
    var page;
    page = Number(that.data.page) + 1; 
    App._post('index/scoreGoods',{page:page,type:2},function(res){
      if(res.code === 1){
        if(res.data.Randomlist.length === 0){
          // that.setData({
          //   text:"已经到底了"
          //  }) 
          
        }else{
          that.setData({
            randomlist:that.data.randomlist.concat(res.data.Randomlist),
            page:page,
          })  
        }
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log('target',res)
    }
    return {
      title: "万步同城",
      desc: "哒哒哒哒哒哒多",
      path: "/pages/wolk/index?user_id="+App.globalData.userInfo.user_id
    };
  },
  changejf:function(){
    let that = this;
    App._post('wolk/changeJf', {wolknum:that.data.wolk_num}, function (result) {
      console.log(result)
      wx.showModal({
       title: '温馨提示',
       content: result.msg,
       showCancel: false,
       success: function (res) {
        return true;
       }
     });
      that.setData({
        score:"今日已兑换",
      })
     });
  },
  getWolknum:function(){
    let that = this;
    wx.getWeRunData({
      success (res) {
        // 拿 encryptedData 到开发者后台解密开放数据
        const encryptedData = res.encryptedData
        App._post('wolk/getWolkdata', { encryptedData: encryptedData,iv:res.iv}, function (result) {
         console.log(result)
         wx.showModal({
          title: '温馨提示',
          content: result.msg,
          showCancel: false,
          success: function (res) {
           return true;
          }
        });
         that.setData({
           wolk_num:result.data.wolk_num,
           score:result.data.score,
           on_off:result.data.on_off,
           texts:result.data.text
         })
        });
      }
    })
  },
  getScore:function(){
   //点击后去打开红包 获取积分
   let that = this;
  App._post('wolk/shareScore', {}, function (result) {
   if(result.code == 1){
      that.setData({
      red:0
    })
   }
    wx.showModal({
      title: '温馨提示',
      content: result.msg,
      showCancel: false,
      success: function (res) {
        return true;
      }
    });
  });
  }
})